package com.zanbo.hot100.permutations;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zanbo
 * @program Solution
 * @since 2021-12-23 下午 1:43
 */
public class Permutations {
    public static void main(String[] args) {
        Solution solution = new Solution();
        List<List<Integer>> permute = solution.permute(new int[]{1, 2, 3});
        for (List<Integer> integers : permute) {
            System.out.println(integers);
        }
    }
}

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        backtrack(nums, res, new ArrayList<>(), new int[nums.length]);
        return res;
    }

    public void backtrack(int[] nums, List<List<Integer>> res, List<Integer> temp, int[] ed) {
        if (temp.size() == nums.length) {
            res.add(new ArrayList<>(temp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (ed[i] == 1) {
                continue;
            }
            ed[i] = 1;
            temp.add(nums[i]);
            backtrack(nums, res, temp, ed);
            ed[i] = 0;
            temp.remove(temp.size() - 1);
        }
    }
}